package wan.com.templateproject.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @author wan
 * @version 1.0.0
 * @Description rabbitMqtt配置类
 * @createTime 2021年11月04日 16:06:41
 */
@Component
public class RabbitMqttConfig {

//    @Value("{spring.rabbit.port}")
    private Integer port = 5672;

//    @Value("{spring.rabbit.host}")
    private String host = "139.224.22.145";

//    @Value("{spring.rabbit.username}")
    private String userName = "rabbitmq";

//    @Value("{spring.rabbit.password}")
    private String passWord = "123456";

//    @Bean
//    public Queue helloQueue() {
//        return new Queue("hello");
//    }

    @Bean
    @Primary
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        //设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }

    @Bean
    @Primary
    public ConnectionFactory connectionFactoryConfigurer() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(passWord);
        return connectionFactory;
    }

}
